<?php
/**
 * Created by Dumitru Russu
 * User: Dumitru Russu
 * Date: 8/20/12
 * Time: 1:45 PM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Locale_Countries extends Page
{
    /**
     * @var Db_Locale_Countries
     */
    protected $tableObject;

    function __construct(){
        $countryId = (int)str_replace('c_name_', '', post('id'));
        $this->idTableObject = (!empty($countryId) ? $countryId : get('id', 'int') );
        $this->actionsTableObject = array(
            'saveAction',
            'configDeliveryAction',
            'addEditConfigDeliveryAction'
        );
        $this->classTableObject = 'Db_Locale_Countries';

        parent::__construct();
    }

    public function defaultAction(){
		if (perm('COUNTRIES_ADD')) {
        	$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=add');
		}
        self::$TPL->appendJsFile('admin/countries');
        $allCountry = Db_Locale_Countries::findAllArray();

        self::$TPL->assign('allCountries', $allCountry);
        self::$TPL->setBodyTemplate('list_countries');

        return array(
            'type' => 'display'
        );
    }

    public function addAction(){
		perm('COUNTRIES_ADD', self::$PAGE['p_url']);
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=add', true);
		self::$TPL->setBodyTemplate('add_country');
		return array('type' => 'display');
    }

    public function saveAction(){
		perm('COUNTRIES_ADD', self::$PAGE['p_url']);
        $countryName = strtolower(post('c_name', 'string'));
        $countryName = (!empty($countryName) ? $countryName : strtolower(post('value', 'string')));
        $countryIdentifier = strtolower(post('c_identifier', 'string'));

        if($this->tableObject->objectArray->id) {
            $this->tableObject->c_name = $countryName;
            if(!$this->tableObject->save()) {
                return array(
                    'type' => 'string',
                    'content' => t(T_COUNTRY_TITLE, $this->tableObject->objectArray->c_name)
                );
            } else {
                return array(
                    'type' => 'string',
                    'content' => t(T_COUNTRY_TITLE, $countryName)
                );
            }
        } else {
            if(!empty($countryName) && !empty($countryIdentifier)){
                $this->tableObject->c_name = $countryName;
                $this->tableObject->c_identifier = $countryIdentifier;
                $this->message = t(T_FORM_MSG, 'success');
                if(!$this->tableObject->save()){
                    $this->error = true;
                    $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
                }
            } else {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'required_fields');
            }
            return array(
                'type' => 'redirect',
                'query' => false
            );
        }
    }

    public function getNameAction() {
		perm('COUNTRIES_ADD', self::$PAGE['p_url']);
		$countryId = (int)str_replace('c_name_', '', get('id'));
		$countries = new Db_Locale_Countries($countryId);
		return array(
            'type' => 'string',
            'content' => $countries->objectArray->c_name
        );
    }
}
